const app = getApp()
const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
const recorderManager = wx.getRecorderManager();
var recordInterval = null;
Page({
  data: {
    themeid: "",
    answerInfo: {}
  },
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      themeid: options.themeid
    })
    // 获取主题信息
    _this.getThemeGoodsInfo();
  },

  /**
   * 获取商品详细信息
   */
  getThemeGoodsInfo: function(){
    var _this = this;
    var themeid = _this.data.themeid;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.themeid = themeid;
        data.access_token = res.access_token;
        util.post(api.Interest_GetThemeGoodsInfoApi, data).then(res => {
          if (res.code == 200) {
            var answerInfo = res.result;
            var dataInfoList = answerInfo.dataInfoList;
            for (var i = 0; i < dataInfoList.length; i++) {
              var dataInfo = dataInfoList[i];
              switch (dataInfo.type) {
                case 3:
                  dataInfo.startDate = "00:00";
                  dataInfo.voiceBoFangImage = api.AudioNoBoFangImage_Name;
                  dataInfo.currentTime = 0;
                  break;
                case 4:
                  var videoid = "myVideo" + new Date().getTime() + i;
                  dataInfo.videoid = videoid;
                  break;
              }
              dataInfoList[i] = dataInfo;
            }
            answerInfo.dataInfoList = dataInfoList;
            _this.setData({
              answerInfo: answerInfo
            })
          }
        })
      })
    }
  },

  /**
   * 目录 
   */
  directory: function (e) {
    var _this = this;
    var poTopViewIndex = e.currentTarget.dataset.type;
    wx.redirectTo({
      url: '../../../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?poTopViewIndex=' + poTopViewIndex +'&circleid=' + _this.data.answerInfo.ordTheme.circleid,
    })
  },

  /**
   * 跳转到地址设置
   */
  navigateToAddrsetup: function(){
    wx.navigateTo({
      url: '../../../../../ucenter/pages/ucenter/personinfo/homesetup/addrsetup/addrsetup',
    })
  },

  /**
   * 下一个主题
   */
  nextTurnTheme: function () {
    var _this = this;
    var nextTurnThemeType = _this.data.answerInfo.nextTurnThemeType;
    if (nextTurnThemeType == 0) {
      return;
    }
    var themeid = _this.data.answerInfo.nextTheme.id;
    if (nextTurnThemeType == 1) {
      // 跳转到回答页
      wx.redirectTo({
        url: '../themeDetails/themeDetails?themeid=' + themeid + '&preview=0',
      })
    } else if (nextTurnThemeType == 2) {
      //跳转到答案页
      wx.redirectTo({
        url: './themeDetails/themeResult/themeResult?themeid=' + themeid + '&previewType=' + _this.data.previewType,
      })
    } else if (nextTurnThemeType == 3) {
      // 跳转到宝贝详情页面
      wx.redirectTo({
        url: '../goodsDetails/goodsDetails?themeid=' + themeid,
      })
    }
  },

  /**
   * 上一个主题
   */
  lastTurnTheme: function () {
    var _this = this;
    var lastTurnThemeType = _this.data.answerInfo.lastTurnThemeType;
    if (lastTurnThemeType == 0) {
      return;
    }
    var themeid = _this.data.answerInfo.lastTheme.id;
    if (lastTurnThemeType == 1) {
      // 跳转到回答页
      wx.redirectTo({
        url: '../themeDetails/themeDetails?themeid=' + themeid + '&preview=0',
      })
    } else if (lastTurnThemeType == 2) {
      //跳转到答案页
      wx.redirectTo({
        url: '../themeDetails/themeResult/themeResult?themeid=' + themeid + '&previewType=' + _this.data.previewType,
      })
    } else if (lastTurnThemeType == 3) {
      // 跳转到宝贝详情页面
      wx.redirectTo({
        url: '../goodsDetails/goodsDetails?themeid=' + themeid,
      })
    }
  },


  /**
   * 去打卡
   */
  doClock: function () {
    var _this = this;
    var themeid = _this.data.answerInfo.ordTheme.id;
    var themeContentType = _this.data.answerInfo.ordTheme.themeContentType;
    var circleid = _this.data.answerInfo.ordTheme.circleid;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.circleid = circleid;
        data.themeid = themeid;
        data.access_token = res.access_token;
        util.post(api.Interest_CheckUserThemeClockApi, data).then(function (res) {
          if (res.code == 200) {
            var result = res.result;
            var type = result.type;
            var message = result.message;
            if (type == 2) {
              //不可以进行打卡
              util.showToast(message, "none");
            }else{
              //可以进行打卡 
              wx.navigateTo({
                url: '../../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/noThemeClock/noThemeClock?themeid=' + themeid + '&circleid=' + circleid,
              })
            }
          } else {
            util.showToast(res.message, "none");
          }
        })
      })
    }
  },
})